package com.istock.servicesample.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import com.istock.base.logtrace.filter.TraceNoFilter;
import com.istock.base.web.filter.HttpTraceFilter;
import com.istock.base.web.filter.XssSqlFilter;
import com.istock.base.web.servlet.HealthCheckServlet;

@Configuration
public class FilterConfig {

	@Bean
	public FilterRegistrationBean xssFilterRegistration()
	{
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		XssSqlFilter filter = new XssSqlFilter();
//		filter.setExcludeRegx("排除url正则");//requestURI
		registrationBean.setFilter(filter);
		List<String> urlPatterns = new ArrayList<String>();
	    urlPatterns.add("/*");
	    registrationBean.setUrlPatterns(urlPatterns);
	    registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
//	    registrationBean.setOrder(1);
		return registrationBean;
	}
	
	@Bean
	public FilterRegistrationBean logFilterRegistration()
	{
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		TraceNoFilter filter = new TraceNoFilter();
		registrationBean.setFilter(filter);
		List<String> urlPatterns = new ArrayList<String>();
	    urlPatterns.add("/*");
	    registrationBean.setUrlPatterns(urlPatterns);
	    registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 2);
		return registrationBean;
	}
	
	@Bean
	public FilterRegistrationBean traceFilterRegistration()
	{
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		HttpTraceFilter filter = new HttpTraceFilter();
		registrationBean.setFilter(filter);
		List<String> urlPatterns = new ArrayList<String>();
	    urlPatterns.add("/*");
	    registrationBean.setUrlPatterns(urlPatterns);
	    registrationBean.setOrder(2);
		return registrationBean;
	}
	
	@Bean
	public ServletRegistrationBean healthCheckRegistration(){
		ServletRegistrationBean registrationBean = new ServletRegistrationBean();
		registrationBean.setServlet(new HealthCheckServlet());
		registrationBean.addUrlMappings("/_health_check.ttt");
		return registrationBean;
	}
}
